package com.wa.controller;
import com.alibaba.excel.EasyExcel;
import com.wa.excel.CustomSheetWriteHandler;
import com.wa.excel.DownloadData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;

@Slf4j
@RestController
public class ExcelController {



    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), DownloadData.class)
                .inMemory(Boolean.TRUE)
                .registerWriteHandler(new CustomSheetWriteHandler())
                .sheet("测试模板")
                .doWrite(new ArrayList());
    }

}
